package com.jarvis.hospital;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jarvis.hospital.common.BaseActivity;
import com.jarvis.hospital.common.ResponseData;
import com.jarvis.hospital.login.model.LoginModel;
import com.jarvis.hospital.login.model.UserModel;
import com.jarvis.hospital.patient.MainPatientActivity;
import com.jarvis.hospital.prescription.AllPrescriptionListActivity;
import com.jarvis.hospital.prescription.BloodDoneListActivity;
import com.jarvis.hospital.prescription.DefaultPrescriptionListActivity;
import com.jarvis.hospital.prescription.NewPrescriptionActivity;
import com.jarvis.hospital.prescription.SpdActivity;
import com.jarvis.hospital.prescription.model.UnreadCountModel;
import com.jarvis.hospital.servers.ApiService;
import com.jarvis.hospital.util.SharePreferenceKey;
import com.jarvis.hospital.util.SharePreferenceUtils;
import com.jarvis.hospital.util.net.RequestApi;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.lin_top_1)
    LinearLayout lin_top_1;
    @BindView(R.id.lin_top_2)
    LinearLayout lin_top_2;
    @BindView(R.id.lin_top_3)
    LinearLayout lin_top_3;
    @BindView(R.id.lin_message)
    LinearLayout lin_message;
    @BindView(R.id.lin_spd)
    LinearLayout lin_spd;
    @BindView(R.id.lin_blood)
    LinearLayout lin_blood;
    @BindView(R.id.lin_cfjl)
    LinearLayout lin_cfjl;
    @BindView(R.id.lin_brgl)
    LinearLayout lin_brgl;
    @BindView(R.id.lin_jkcf)
    LinearLayout lin_jkcf;
    @BindView(R.id.tv_record)
    TextView tv_record;
    @BindView(R.id.tv_message)
    TextView tv_message;

    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.rel_right)
    RelativeLayout rel_right;
    @BindView(R.id.tv_right)
    TextView tv_right;

    private ApiService service;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        service = RequestApi.create(ApiService.class);
        Bundle bundle = this.getIntent().getExtras();
        boolean isAuto = bundle.getBoolean("isAuto");
        if (isAuto) {
//            UpdateManager manager = new UpdateManager(this);
//            manager.checkUpdate();
        }
        UserModel userModel = SharePreferenceUtils.getObject(SharePreferenceKey.LOGIN_MODEL, LoginModel.class).getUserInfo();
        String userRole = userModel.getUserRole();
        if("clinicalPharmacist".equals(userRole)){
            lin_top_2.setVisibility(View.GONE);
            lin_top_3.setVisibility(View.VISIBLE);
        }else if("doctor".equals(userRole)){
            lin_top_3.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUnreadCount();
    }

    @Override
    public void initView() {
        // setStatus(true);
        lin_message.setOnClickListener(this);
        lin_spd.setOnClickListener(this);
        lin_blood.setOnClickListener(this);
        lin_cfjl.setOnClickListener(this);
        lin_brgl.setOnClickListener(this);
        lin_jkcf.setOnClickListener(this);
        rel_right.setOnClickListener(this);
        rel_left.setVisibility(View.GONE);
        rel_right.setVisibility(View.VISIBLE);
        tv_right.setText("设置");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_right:
                startActivity(SettingActivity.class);
                break;
            case R.id.lin_spd:
                startActivity(SpdActivity.class);
                break;
            case R.id.lin_blood:
                startActivity(BloodDoneListActivity.class);
                break;
            case R.id.lin_message:
                Bundle bundle = new Bundle();
                bundle.putString("type", "message");
                bundle.putBoolean("isAuto", false);
                startActivity(MainPatientActivity.class, bundle);
                break;
            case R.id.lin_cfjl:
                Bundle bundless = new Bundle();
                bundless.putBoolean("isAuto", false);
                startActivity(AllPrescriptionListActivity.class,bundless);
                break;
            case R.id.lin_brgl:
                Bundle bundles = new Bundle();
                bundles.putString("type", "brgl");
                bundles.putBoolean("isAuto", false);
                startActivity(MainPatientActivity.class, bundles);
                break;
            case R.id.lin_jkcf:
                Bundle b = new Bundle();
                b.putString("programmeId","-1");
                b.putInt("type",1);
                startActivity(NewPrescriptionActivity.class, b);
                break;
        }
    }

    public void getUnreadCount() {
        showDialog("加载中");
        service.getUnreadCount()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new Observer<ResponseData<UnreadCountModel>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData<UnreadCountModel> unreadCountModelResponseData) {
                        if (unreadCountModelResponseData != null) {
                            int resultCode = unreadCountModelResponseData.getResultCode();
                            if (resultCode == 200) {
                                if (unreadCountModelResponseData.getData().getUnReadMsg() == 0) {
                                    tv_message.setVisibility(View.GONE);
                                } else {
                                    tv_message.setVisibility(View.VISIBLE);
                                    tv_message.setText(unreadCountModelResponseData.getData().getUnReadMsg() + "");
                                }
                                if (unreadCountModelResponseData.getData().getUnReadCountPrescription() == 0) {
                                    tv_record.setVisibility(View.GONE);
                                } else {
                                    tv_record.setVisibility(View.VISIBLE);
                                    tv_record.setText(unreadCountModelResponseData.getData().getUnReadCountPrescription() + "");
                                }
                            } else {
                                showTextToast(unreadCountModelResponseData.getMessage());
                            }
                        } else {
                            showTextToast("请求失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        disMiss();
                        showTextToast(RequestApi.detailError(e));
                    }

                    @Override
                    public void onComplete() {
                        disMiss();
                    }
                });

    }
}
